import React from "react";
import { Link, Routes, Route, useParams } from "react-router-dom";

const Home = () => {
  return <div>这是home页面</div>;
};

const About = () => {
  return <div>这是about页面</div>;
};

const Detail = () => {
  const params = useParams();
  console.log(params);
  return <div>这是详情页面 - {params.id}</div>;
};

const App = () => {
  return (
    <>
      <h2>react-router-v6-动态路由</h2>
      <ul>
        <li>
          <Link to="/home">home</Link>
        </li>
        <li>
          <Link to="/about">about</Link>
        </li>
        <li>
          <Link to="/detail/123">detail</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        {/* 动态路由的写法和之前也是一样的 */}
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        {/* 动态可选参数 */}
        {/* 参数有和没有页面都能显示 */}
        <Route path="/detail" element={<Detail />}>
          <Route path=":id" element={<Detail />}></Route>
        </Route>
        <Route path="*" element={<div>404 font found...</div>}></Route>
      </Routes>
    </>
  );
};

export default App;
